package com.supplychainapi.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.supplychainapi.config.datasource.ProdDBConfig;

@Configuration
@MapperScan(basePackages = "com.supplychainapi.dao.prod", sqlSessionTemplateRef = "prodSqlSessionTemplate")
public class ProdMyBatisConfig {

	@Primary
	@Bean(name = "prodDataSource")
	public DataSource prodDataSource(ProdDBConfig prodDBConfig) throws SQLException {
		
		DruidXADataSource druidXADataSource = new DruidXADataSource();
		druidXADataSource.setUrl(prodDBConfig.getUrl());
		druidXADataSource.setUsername(prodDBConfig.getUsername());
		druidXADataSource.setPassword(prodDBConfig.getPassword());
		druidXADataSource.setDriverClassName(prodDBConfig.getDriverClassName());
		druidXADataSource.setInitialSize(prodDBConfig.getInitialSize());
		druidXADataSource.setMinIdle(prodDBConfig.getMinIdle());
		druidXADataSource.setMaxActive(prodDBConfig.getMaxActive());
		druidXADataSource.setMaxWait(prodDBConfig.getMaxWait());
		druidXADataSource.setTimeBetweenEvictionRunsMillis(prodDBConfig.getTimeBetweenEvictionRunsMillis());
		druidXADataSource.setValidationQuery(prodDBConfig.getValidationQuery());
		druidXADataSource.setTestOnBorrow(prodDBConfig.isTestOnBorrow());
		druidXADataSource.setTestWhileIdle(prodDBConfig.isTestWhileIdle());
		druidXADataSource.setRemoveAbandoned(prodDBConfig.isRemoveAbandoned());
		druidXADataSource.setRemoveAbandonedTimeout(prodDBConfig.getRemoveAbandonedTimeout());
		
		String filters = prodDBConfig.getFilters();
		// 配置监控统计拦截的filters不为空，设置监控信息
		if(StringUtils.isNotBlank(filters)) {
			druidXADataSource.setFilters(prodDBConfig.getFilters());
			druidXADataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
			// 60秒一次，保存日志到log4j2中
			druidXADataSource.setTimeBetweenLogStatsMillis(60000);
			DruidDataSourceStatLogger dataSourceStatLogger = new DruidStatLogger();
			druidXADataSource.setStatLogger(dataSourceStatLogger);
		}
		
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(druidXADataSource);
		xaDataSource.setUniqueResourceName("prodDataSource");
		xaDataSource.setPoolSize(prodDBConfig.getMinIdle());
		xaDataSource.setMinPoolSize(prodDBConfig.getMinIdle());
		xaDataSource.setMaxPoolSize(prodDBConfig.getMaxActive());
		xaDataSource.setBorrowConnectionTimeout(60);
		xaDataSource.setMaxIdleTime(60);
		xaDataSource.setMaintenanceInterval(60);
		xaDataSource.setTestQuery(prodDBConfig.getValidationQuery());
		return xaDataSource;
	}

	@Bean(name = "prodSqlSessionFactory")
	public SqlSessionFactory prodSqlSessionFactory(@Qualifier("prodDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/prod/*.xml"));
//		org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
//		config.setMapUnderscoreToCamelCase(false);
//		bean.setConfiguration(config);
		return bean.getObject();
	}

	@Bean(name = "prodSqlSessionTemplate")
	public SqlSessionTemplate prodSqlSessionTemplate(
			@Qualifier("prodSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
